package com.gvitz.transaction.utils;

import java.util.ArrayList;

import com.gvitz.transaction.R;
import com.gvitz.transaction.models.Card;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


/** Clase Adapter para ver la lista de los Cards
 * @author Orlando
 *
 */
public class CardsItemAdapter extends ArrayAdapter<Card> {
	private ArrayList<Card> Cards;
	private Activity act;

	public CardsItemAdapter(Context context, int textViewResourceId, ArrayList<Card> mCards,Activity activity) {
		super(context, textViewResourceId, mCards);
		this.Cards = mCards;
		this.act = activity;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.card_item, null);
		}
		
		Card tarjeta = Cards.get(position);
		
		if (tarjeta  != null) {
			
			TextView titulo = (TextView) v.findViewById(R.id.titulo_item_card);
			RelativeLayout tipo = (RelativeLayout) v.findViewById(R.id.fondo_tarjeta);
			ImageView foto= (ImageView) v.findViewById(R.id.logo_card);
			TextView number = (TextView) v.findViewById(R.id.number_item_card);

			if (tarjeta.getType().equals("Visa")){
				tipo.setBackgroundResource(R.drawable.layout_visa);
				foto.setImageResource(R.drawable.visa);
			}
			else{
				if (tarjeta.getType().equals("MasterCard")){
				   tipo.setBackgroundResource(R.drawable.layout_master);
				   foto.setImageResource(R.drawable.master);
				}
				else{
					if (tarjeta.getType().equals("American Express")){
						 tipo.setBackgroundResource(R.drawable.layout_american);
						 foto.setImageResource(R.drawable.amex);
					}
					else{
						if (tarjeta.getType().equals("Discover")){
							 tipo.setBackgroundResource(R.drawable.layout_discover);
							 foto.setImageResource(R.drawable.discover);
						}
						else{
							 tipo.setBackgroundResource(R.drawable.layout_debito);
							 foto.setImageResource(R.drawable.debito1);
						}
						
					}
					
				}
				
			}
			
			
			if (titulo != null) {
				titulo.setText(tarjeta .getNombre());
			}
								
			if (number  != null) {
				number.setText(" ****  ****  ****  " + tarjeta.getCard_number());
			}


		}
		return v;
	}
}
